<?php
/**
 * @brief Klasa kontrolera index
 *
 */
class IndexController extends Zend_Controller_Action
{

    /**
     * @brief Metoda wyświetlajaca nagłówek strony
     *
     */
    public function preDispatch()
    {
        $this->view->render('utility/_headbox.phtml');
    }

    /**
     * @brief Akcja index
     *
     * Pełni funkcję ozdobną
     *
     */
    public function indexAction()
    {
            $mysession = new Zend_Session_Namespace('mysession');
            $auctionId = 19;
            $max = 100;
            $file = 'temp/wyniki_aukcja'.$auctionId.'_nr'.$mysession->uniqid.'.txt';
            for($i = 1; $i <= $max; $i++) {
                if($i%1 == 0) {
                    $string = $i.'|'.
                        intval($mysession->step1[$i]*1000) .'|'.
                        intval($mysession->step2[$i]*1000) .'|'.
                        intval($mysession->step3[$i]*1000) .'|'.
                        intval($mysession->step4[$i]*1000) .'|'.
                        intval($mysession->step5[$i]) ."\n";
                
                    file_put_contents($file, $string, FILE_APPEND);       
                }
            }
    }
    
     public function testAction ()
     {
        $mysession = new Zend_Session_Namespace('mysession');
        $auctionId = 19;
        $max = 100;
        $mysession->auctionId = $auctionId;
        //$userId = 'admin';
        
        $ids = array(
            'Admin',
            'Marcin',
            'Radek',
            'Karol',
            'Rafał',
            'Robert',
            'qwerty'
        );
        $userId = $ids[array_rand($ids)];
        //throw new Exception("$userId");
        if (!isset($mysession->counter)) {
            $mysession->counter = 1;
            $mysession->uniqid = uniqid();
            $this->addCommoditiesToAuction($auctionId);
        } else {
            
            $this->view->refresh = true;
            $this->createSimpleOffert($auctionId, $userId);
            //$this->createComplexOffert($auctionId, $userId);
            //$this->createComplexOffert2($auctionId, $userId);
            if( $mysession->counter%1 == 0) {
                $balance = new Application_Model_Balancing($auctionId);
                $balance->makeMagic();
            }
            $mysession->counter++;
            echo 'transformacja</br>';
            print_r($mysession->step1);
            echo '</br>xslt</br>';
             print_r($mysession->step2);
            echo '</br>solver</br>';
             print_r($mysession->step3);
            echo '</br>full</br>';
             print_r($mysession->step4);
        }
        
        
        if ($mysession->counter > $max) {
            $this->view->refresh = false;
            $file = 'temp/wyniki_aukcja'.$auctionId.'_nr'.$mysession->uniqid.'.txt';
            for($i = 1; $i <= $max; $i++) {
                if($i%1 == 0) {
                    $string = $i.'|'.
                        intval($mysession->step1[$i]*1000) .'|'.
                        intval($mysession->step2[$i]*1000) .'|'.
                        intval($mysession->step3[$i]*1000) .'|'.
                        intval($mysession->step4[$i]*1000) .'|'.
                        intval($mysession->step5[$i]) ."\n";
                
                    file_put_contents($file, $string, FILE_APPEND);       
                }
            }
            unset($mysession->counter);
            unset($mysession->step1);
            unset($mysession->step2);
            unset($mysession->step3);
            unset($mysession->step4);
        }      
        //$this->createSimpleOffert($auctionId, $userId);
        //$this->createComplexOffert($auctionId, $userId);
        //$this->createComplexOffert2($auctionId, $userId);
        
        
        
//        $info = array();
//        $info['commodities']='';
//        $info['offers']='';
//        $info['groups']=array();
//        $info = $offer->toXML($info);
//        $this->view->info = $info;
     }

    /**
     * @brief Metoda wyświetlająca stopkę strony
     *
     */
    public function postDispatch()
    {
        $this->view->render('utility/_footbox.phtml');
        
    }
    
    public function createSimpleOffert($auctionId,$userId) {
        $this->createOffer($auctionId, $userId);
    }
    
    public function createComplexOffert($auctionId, $userId) {
        $headId = $this->createOffer($auctionId, $userId);
        
        $group1 = $this->createGroup($userId, $headId, $headId);
        $offer1_1 = $this->createOffer($auctionId, $userId, $group1, $headId);
        $offer1_2 = $this->createOffer($auctionId, $userId, $group1, $headId);
        $group2 = $this->createGroup($userId, $headId, $headId);
        $offer2_1 = $this->createOffer($auctionId, $userId, $group2, $headId);
        $offer2_2 = $this->createOffer($auctionId, $userId, $group2, $headId);
    }
    
    public function createComplexOffert2($auctionId,$userId) {
        $headId = $this->createOffer($auctionId, $userId);
        
        $group1 = $this->createGroup($userId, $headId, $headId);
        $offer1_1 = $this->createOffer($auctionId, $userId, $group1, $headId);
        $offer1_2 = $this->createOffer($auctionId, $userId, $group1, $headId);
        $group2 = $this->createGroup($userId, $headId, $headId);
        $offer2_1 = $this->createOffer($auctionId, $userId, $group2, $headId);
        $offer2_2 = $this->createOffer($auctionId, $userId, $group2, $headId);
        
        $group3 = $group1 = $this->createGroup($userId, $offer1_1, $headId);
        $offer3_1 = $this->createOffer($auctionId, $userId, $group3, $headId);
        $offer3_2 = $this->createOffer($auctionId, $userId, $group3, $headId);

        $group4 = $group1 = $this->createGroup($userId, $offer1_2, $headId);
        $offer4_1 = $this->createOffer($auctionId, $userId, $group4, $headId);
        $offer4_2 = $this->createOffer($auctionId, $userId, $group4, $headId);

        $group5 = $group1 = $this->createGroup($userId, $offer2_1, $headId);
        $offer5_1 = $this->createOffer($auctionId, $userId, $group5, $headId);
        $offer5_2 = $this->createOffer($auctionId, $userId, $group5, $headId);
        
        $group6 = $group1 = $this->createGroup($userId, $offer2_2, $headId);
        $offer6_1 = $this->createOffer($auctionId, $userId, $group6, $headId);
        $offer6_2 = $this->createOffer($auctionId, $userId, $group6, $headId);

    }
    
    public function createOffer($auctionId, $userId, $groupId = null,$headId = null) {
        $offer = new Application_Model_Offer();
        $offer->setMinVolume(0);
        $offer->setMaxVolume(1);
        $offer->setOfferedPrice(rand(0, 1111));
        $offer->setMarketEntity_id($userId);
        $mapper = new Application_Model_OfferMapper();
        if(empty($groupId)) { 
            $offer->setFirstOffer(1);
            $offer->setAuction_id($auctionId);                
        } else {
            $offer->setFirstOffer(0);
            $offer->setOwnerGroup_id($groupId);
            $offer->setHeadOffer_id($headId);
            $offer->setAuction_id($auctionId);
        }
        $newId = $mapper->save($offer);
        //dodaj komponenty
        if(rand(0,1)){
            $this->createComponent($newId);
        } else {
            $this->createComponent($newId);
            try {
                $this->createComponent($newId);
            } catch (Exception $exc) {
                echo 'dwa razy ten sam towar';
            }
        }
        return $newId;
    }

    public function createGroup($userId, $offerId, $headId) {
        $mapper = new Application_Model_GroupMapper();
        $group = new Application_Model_Group();
        $group->setIsMandatory(1);
        $group->setMultipler(1);
        $group->setHeadOffer_id($headId);
        $group->setMarketEntity_id($userId);
        $id = $mapper->save($group);
        
        //dodac zapis oferty
        $affiliationMapper = new Application_Model_GroupAffiliation();
        $affiliationMapper->save($id, $offerId);
        return $id;
    }

    public function createComponent($offerId) {
        $commodityId = (rand(48, 65));
        $component = new Application_Model_OfferComponent();
        $component->setCommodity_id($commodityId);
        $component->setQuantity(rand(1,4));
        if(rand(0,1)){
            $component->setShareFactor(1);
        } else {
            $component->setShareFactor(-1);
        }
        $component->setOffer_id($offerId);
        $mapper = new Application_Model_OfferComponentMapper();
        $mapper->save($component);
    }

    public function addCommoditiesToAuction($auctionId) {
        $mapper = new Application_Model_CommodityAffiliationMapper();
        $data = array(
            'Auction_id'    =>$auctionId,
            'Commodity_id'  => 0,
            'shareFactor'   => -1);
        for($i = 48; $i <= 65; $i++) {
            $data['Commodity_id'] = $i;
            try {
                $mapper->save($data);
            } catch (Exception $exc) {
                echo "towar $i już jest w aukcji</br>";
            }    
        }
    }

}

